<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/5
 * Time: 19:03
 */

namespace app\api\controller;

use think\Db;
use think\Request;

class Cashchild extends Base{

    /**
     * Notes: 添加商户子账号
     * Create Date: 2018/10/5
     * Create by :lijie
     * 参数：
     */
    public function addCashChild(Request $request){

        $param = $request->post();
        $resVali = $this->validate($param,"CashchildValidate.addCashChild");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }

        $a_id           = $param['a_id'];
        $account        = $param['name'];
        $password       = $param['pass'];
        $new_password   = $param['checkPass'];
        // 判断子商户手机号是否存在
        $resPhone = Db::name('cash_child')->where(['account'=>$account,'status'=>0])->field("account")->find();
        if($resPhone){
            return apiResponse(201,"手机号已经存在",'');
        }
        // 判断两次密码是否一致
        if($password != $new_password){
            return apiResponse(201,'两次密码不同','');
        }

        $save['account']    = $account;
        $save['password']   = md5($password);
        $save['ctime']      = time();
        $save['utime']      = time();
        $save['status']     = 0;
        $save['a_id']       = $a_id;

        $res = Db::name('cash_child')->insert($save);
        if($res){
            return apiResponse(200,'添加成功',$res);
        }else {
            return apiResponse(200,'添加失败','');
        }
    }

    /**
     * Notes: 删除商户子账号
     * Create Date: 2018/10/5
     * Create by :lijie
     * 参数：1,2,3
     */
    public function delCashChild(Request $request){

        $param      = $request->post();
        $resVali    = $this->validate($param,"CashchildValidate.delCashChild");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }

        $ids = $param['ids'];
        $ids = explode(',',$ids);

        $c_id = count($ids);
        for ($i=0;$i<$c_id;$i++){
            $res = Db::name('cash_child')->where('id',$ids[$i])->update(['status'=>9]);

        }
        if($res){
            return apiResponse(200,'删除成功',$res);
        }else {
            return apiResponse(200,'删除失败','');
        }
    }

    /**
     * Notes: 修改商户子账号
     * Create Date: 2018/10/5
     * Create by :lijie
     * 参数：
     */
    public function editCashChild(Request $request){
        // 验证
        $param   = $request->post();
        $resVali = $this->validate($param,"CashchildValidate.editCashChild");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        $id      = input('post.id');
        $account = input('post.name');
        $pwd     = input('post.pass');
        $new_pwd = input('post.checkPass');

        if($pwd != $new_pwd){
            return apiResponse(201,'两次密码不同','');
        }

        if(!empty($account)){
            $w['account'] = $account;
        }
        if(!empty($pwd)){
            $w['password'] = md5($pwd);
        }
        $w['utime'] = time();

        $res = Db::name('cash_child')->where('id',$id)->update($w);
        if($res){
            return apiResponse(200,'修改成功',$res);
        }else {
            return apiResponse(201,'修改失败','');
        }
    }

    /**
     * Notes: 商户子账号列表
     * Create Date: 2018/10/5
     * Create by :lijie
     * 参数：
     */
    public function cashChildList(){
        $id = input('post.id');
        if($id){
            $cash = Db::name('cash_child')->where('id',$id)->find();
            if($cash){
                $cash['ctime'] = date("Y-m-d H:i:s",$cash['ctime']);
                $cash['utime'] = date("Y-m-d H:i:s",$cash['utime']);
                return apiResponse(200,'获取成功',$cash);
            }else {
                return apiResponse(201,'获取失败',$cash);
            }
        }else {
            $page = input('post.page');
            $pageNum = input('post.pageNum');
            $page = empty($page) ? 1 : $page;
            $pageNum = empty($pageNum) ? 5 : $pageNum;
            $a_id = input('post.a_id');

            if(!empty(input('post.name'))){
                $w['account'] = input('post.name');
            }
            $w['a_id'] = $a_id;
            $w['status'] = 0;
            $total = Db::name('cash_child')->where($w)->count();
            $res   = Db::name('cash_child')->where($w)->order("ctime desc")->page($page,$pageNum)->select();
            if($res){
                $c_cash = count($res);
                for($i=0;$i<$c_cash;$i++){
                    $res[$i]['ctime'] = date("Y-m-d H:i:s",$res[$i]['ctime']);
                    $res[$i]['utime'] = date("Y-m-d H:i:s",$res[$i]['utime']);
                }
                $data['total'] = $total;
                $data['list']  = $res;
                return apiResponse(200,'获取成功',$data);
            }else {
                return apiResponse(201,'获取失败','');
            }
        }
    }

}